package com.haiheng.common.constant

import android.content.Context
import android.os.Build
import android.telephony.TelephonyManager
import android.text.TextUtils
import androidx.core.content.ContextCompat.getSystemService
import com.haiheng.common.api.API
import com.haiheng.common.api.API.PARAM.Companion.PARAGE_CATALOG_TYPE
import com.haiheng.common.api.API.PARAM.Companion.PARAGN_MODE_TYPE
import com.haiheng.library.log.LogUtils
import com.haiheng.library.log.LogWriter
import com.haiheng.library_base.toast.Utils
import com.haiheng.library_base.utils.DateUtils
import com.haiheng.library_base.utils.SPUtils
import com.haiheng.library_base.utils.SystemUtil
import com.ys.rkapi.MyManager
import java.util.Date


/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/5/5 0005
 * @Version: 1.0
 */
class AppConstant {
    companion object{

        val countDownTime = 180
        val defaultReportInterval = 120 //单位s
        const val GPIO_INDEX = 40
        const val INITIAL_PASSWORD = "111111"
        const val DEFAUL_LIBRARY_CODE = "CN-518000-HHLIB"
        const val PARAGM_FACE_DEVICE_FACEL_LIBRARY_CODE = "CN_518000_HHLIB001"
//        const val BASE_WEB_VIEW_URL = "http://172.16.2.177:8000/home"
//        const val BASE_WEB_VIEW_URL = "http://opac.bjmu.edu.cn/home"
//        const val BASE_WEB_VIEW_URL = "http://opac.bjmu.edu.cn/home"
//        const val BASE_WEB_VIEW_URL = "http://soft.imtt.qq.com/browser/tes/feedback.html"
//        const val BASE_WEB_VIEW_URL = "http://www.hangzhou.gov.cn/"
//        const val BASE_WEB_VIEW_URL = "http://172.16.0.137:8083/vufind/"
//        const val BASE_WEB_VIEW_URL = "http://119.147.212.11:8080/home"
//        const val BASE_WEB_VIEW_URL = "https://www.runfastai.com/dh-lib/qwer/sdk-debug"
        const val BASE_WEB_VIEW_URL = "https://www.runfastai.com/dh-lib/xiaoou/home"
        const val PARAM_TOKEN_ACCOUNT = "admin"
        const val PARAM_TOKEN_PASSWORD = "admin"

        const val SP_WEB_VIEW_URL = "webViewUrl"
        const val SP_ADMIN_PASSWORD = "SP_ADMIN_PASSWORD"
        const val SP_OPEN_FACE  = "SP_OPEN_FACE"
        const val SP_WEBVIEW_SIZE  = "SP_WEBVIEW_SIZE"
        const val SP_IS_EXT  = "SP_IS_EXT"
        const val SP_TOKEN = "SpToken"
        const val SP_FACE_TOKEN = "SpFaceToken"
        const val SP_LIBRARYCODE = "SP_LIBRARYCODE"
        const val SP_MACHINE_NAME = "SP_MACHINE_NAME"
        const val SP_MACHINE_MODEL_TYPE = "SP_MACHINE_MODEL_TYPE"
        const val SP_MACHINE_CATALOG_TYPE= "SP_MACHINE_CATALOG_TYPE"
        const val SP_REPORT_INTERVAL= "SP_REPORT_INTERVAL"
        const val SP_SCHEDULED_TASK  = "SP_SCHEDULED_TASK"//定时任务
        const val SP_MQTT_URL = "SP_MQTT_URL"
        const val SP_MQTT_NAME = "SP_MQTT_NAME"
        const val SP_MQTT_PASSWORD = "SP_MQTT_PASSWORD"
        const val SP_FACE_LIBRARY_CODE = "SP_FACE_LIBRARY_CODE"
        const val SP_FACE_BASE_URL = "SP_FACE_BASE_URL"
        const val SP_FACE_TOKEN_ACCOUNT = "SP_FACE_TOKEN_ACCOUNT"
        const val SP_FACE_TOKEN_PASSWORD = "SP_FACE_TOKEN_PASSWORD"
        const val SP_LAST_RESOURCE_BUNDLE_RECORD_ID = "lastResourceBundleRecordId"//人脸同步最后记录
        const val SP_SCAN_PREF_VENDOR_ID = "SP_SCAN_PREF_VENDOR_ID"
        const val SP_SCAN_PREF_PRODUCT_ID = "SP_SCAN_PREF_PRODUCT_ID"
        const val SP_SERIAL_PORNAME = "SP_SERIAL_PORNAME"
        const val SP_SYSTEM_VERSION_CODE = "SP_SYSTEM_VERSION_CODE"
        const val SP_PAGE_COUNTDWN_TIME = "SP_PAGE_COUNTDWN_TIME"
        const val SP_IS_PATRON_PASSWORD_VALID = "SP_IS_PATRON_PASSWORD_VALID"
        const val SP_H5_SAVE_INFO = "SP_H5_SAVE_INFO"
        const val SP_RFID_DEV_RUN_STATUS = "SP_RFID_DEV_RUN_STATUS"
        const val SP_SCAN_DEV_RUN_STATUS = "SP_SCAN_DEV_RUN_STATUS"
        const val SP_CAMERA_DEV_RUN_STATUS = "SP_CAMERA_DEV_RUN_STATUS"
        const val SP_RADAR_DETECTION_DEV_RUN_STATUS = "SP_RADAR_DETECTION_DEV_RUN_STATUS"
        const val SP_MIC_NOISE_REDUCTION_RUN_STATUS = "SP_MIC_NOISE_REDUCTION_RUN_STATUS"


        //mqtt topic
        var TOPIC_COMMANDS_PRE = "\$oc/devices/${getMachineUuid()}/sys/commands"
        var TOPIC_COMMANDS = "${TOPIC_COMMANDS_PRE}/#"
        var TOPIC_COMMANDS_RESPONSE = "\$oc/devices/${getMachineUuid()}/sys/commands/response"
        var TOPIC_PROPERTIEST_PRE = "\$oc/devices/${getMachineUuid()}/sys/properties/set"
        var TOPIC_PROPERTIEST = "${TOPIC_PROPERTIEST_PRE}/#"
        var TOPIC_PROPERTIEST_RESPONSE = "\$oc/devices/${getMachineUuid()}/sys/properties/set/response"
        var TOPIC_EVENTS_DOWN = "\$oc/devices/${getMachineUuid()}/sys/events/down"
        var TOPIC_EVENTS_UP= "\$oc/devices/${getMachineUuid()}/sys/events/up"
        const val QOS = 1
        private const val RECONNECT_DELAY = 5000L // 5 seconds

        fun getLastFaceResRecordId():Int{
            return SPUtils.getInstance()
                .getInt(AppConstant.SP_LAST_RESOURCE_BUNDLE_RECORD_ID,0)
        }
        fun getReportInterval():Int{
            return SPUtils.getInstance().getInt(SP_REPORT_INTERVAL, defaultReportInterval)
        }
        fun getMachineInfo():String{
            var manage = MyManager.getInstance(Utils.getApp())
//            var name = "${manage.androidModle}-${manage.vendorID}-${manage.sn}+${manage.androidVersion}"
            var name = "${manage.sn}-${manage.androidVersion}"
            return name
        }

        fun getMqttName():String{
            return SPUtils.getInstance().getString(SP_MQTT_NAME, "devSyncTest")
        }

        fun getMqttPassword():String{
            return SPUtils.getInstance().getString(SP_MQTT_PASSWORD, "devSync123!Test")
        }

        fun getBrokerUrl():String{
//            return "mqtt://127.0.0.1:9310"
            return SPUtils.getInstance().getString(SP_MQTT_URL, "tcp://172.16.0.118:9310")
        }
        fun getClientId():String{
            var clienId = "${AppConstant.getMachineUuid()}_0_0_${
                DateUtils.formatDateTime(
                    Date(),
                    DateUtils.DATE_FORMAT_9)}"
            return clienId
        }
        fun getMachineUuid():String{
            var mac = SystemUtil.getMacAddress(Utils.getApp())
            var uuid = mac.replace(":","")
            if (TextUtils.isEmpty(uuid)){
                uuid = "1213456789"
            }
            return uuid
        }

        fun getMachineCatalogType():Int{
            return SPUtils.getInstance().getInt(SP_MACHINE_CATALOG_TYPE, PARAGE_CATALOG_TYPE)
        }

        fun getMachineModelType():String{
            return SPUtils.getInstance().getString(SP_MACHINE_MODEL_TYPE,
                PARAGN_MODE_TYPE
            )
        }
        fun getMachineName():String{
            return SPUtils.getInstance().getString(SP_MACHINE_NAME, getMachineInfo())
        }
        fun getLibraryCode():String{
            return SPUtils.getInstance().getString(SP_LIBRARYCODE,DEFAUL_LIBRARY_CODE)
        }

        fun getFaceLibraryCode():String{
            var faceLibraryCode = SPUtils.getInstance().getString(SP_FACE_LIBRARY_CODE,PARAGM_FACE_DEVICE_FACEL_LIBRARY_CODE)
            return faceLibraryCode
        }
        fun getFaceTokenAccount():String{
            var faceTokenAccount = SPUtils.getInstance().getString(SP_FACE_TOKEN_ACCOUNT,PARAM_TOKEN_ACCOUNT)
            return faceTokenAccount
        }

        fun getFacePassword():String{
            var facePassword = SPUtils.getInstance().getString(SP_FACE_TOKEN_PASSWORD,PARAM_TOKEN_PASSWORD)
            return facePassword
        }
        fun getToken():String{
            var token = SPUtils.getInstance().getString(AppConstant.SP_TOKEN)
            return token
        }
        fun getFaceToken():String{
            var token = SPUtils.getInstance().getString(AppConstant.SP_FACE_TOKEN)
            return token
        }


        fun isOpenFace():Boolean{
            var isOpen =  SPUtils.getInstance().getBoolean(SP_OPEN_FACE,false)
            return isOpen
        }
        fun getWebViewUrl():String{
            var url =  SPUtils.getInstance().getString(SP_WEB_VIEW_URL,BASE_WEB_VIEW_URL)
            return url
        }

        fun getAdminPassword():String{
            var password = SPUtils.getInstance().getString(SP_ADMIN_PASSWORD,INITIAL_PASSWORD)
            return password
        }

        fun getWebpageSize():Int{
            var webSize = SPUtils.getInstance().getInt(SP_WEBVIEW_SIZE,100)
            return webSize
        }

        fun isOpenLog() : Boolean{
            var isOpenLog =  SPUtils.getInstance().getBoolean(Keys.SWITCH_OPEN_LOG,true)
            return  isOpenLog
        }
        fun isOpenView() : Boolean{
            var isOpenView =  SPUtils.getInstance().getBoolean(Keys.SWITCH_DEBUG_VIEW,false)
            return  isOpenView
        }

        fun isExitOpenApp():Boolean{
            var isExitOpenApp =  SPUtils.getInstance().getBoolean(Keys.SWITCH_EXIT_OPEN_APP,false)
            return  isExitOpenApp
        }

        fun isUseM20():Boolean{
            return false
        }
        fun getCardSerialPortName(): String? {
            var serialPorname =  SPUtils.getInstance().getString(SP_SERIAL_PORNAME,"/dev/ttyS3")
            LogUtils.e("串口号：${serialPorname}")
            return serialPorname
        }

        fun getSystemBoard():String{
            var systemVersionCode =  SPUtils.getInstance().getString(SP_SYSTEM_VERSION_CODE,"纵云3588")
            return systemVersionCode
        }

        fun isZhongyun3588Board():Boolean{
            return getSystemBoard().equals("纵云3588")
        }
        fun isYSK3588Board():Boolean{//是否亿昇K3588的板子，
            return getSystemBoard().equals("亿昇3588")
        }

        fun isOpenNoiseReduction():Boolean{//是否开启声音降噪
            return SPUtils.getInstance().getBoolean(Keys.SWITCH_OPEN_NOISE_REDUCTION,false)
        }

        fun isUseAIUI():Boolean{//是否使用aiui服务
            return SPUtils.getInstance().getBoolean(Keys.SWITCH_USE_AIUI,false)
        }

        fun isOpenOfflineVoice():Boolean{//是否开启离线拾音
            return SPUtils.getInstance().getBoolean(Keys.SWITCH_OPEN_OFFLINE_VOICE,false)
        }

        fun isShowBackBtn():Boolean{//是否显示返回按钮
            return SPUtils.getInstance().getBoolean(Keys.SP_SHOW_BACK_BUTTON,false)
        }

        fun getPageCountDownTime():Int{
            return SPUtils.getInstance()
                .getInt(AppConstant.SP_PAGE_COUNTDWN_TIME, countDownTime)
        }

        fun isPatronPasswordValid():Boolean{//是否开启密码验证
            return SPUtils.getInstance().getBoolean(AppConstant.SP_IS_PATRON_PASSWORD_VALID,false)
        }

        fun getRfidReaderRunStatus():Int{
            return SPUtils.getInstance().getInt(SP_RFID_DEV_RUN_STATUS)
        }
        fun getScanDevRunStatus():Int{
            return SPUtils.getInstance().getInt(SP_SCAN_DEV_RUN_STATUS)
        }
        fun getCameraDevRunStatus():Int{
            return SPUtils.getInstance().getInt(SP_CAMERA_DEV_RUN_STATUS)
        }
        fun getRadarDetectionRunStatus():Int{
            return SPUtils.getInstance().getInt(SP_RADAR_DETECTION_DEV_RUN_STATUS)
        }
        fun getMicNoiseDevRunStatus():Int{
            return SPUtils.getInstance().getInt(SP_MIC_NOISE_REDUCTION_RUN_STATUS)
        }
    }

    interface SCHEMENAME{
        companion object{
            const val CAMERA = "Camera"
            const val RADARDETECTION = "RadarDetection"
            const val RFIDREADER = "RFIDReader"
            const val AUDIOPICKER = "AudioPicker"
            const val VOICEWAKEUP = "VoiceWakeup"
            const val VOICESTOP = "VoiceStop"
            const val PAUSE_AUDIO_PICKER = "pauseAudioPicker"
            const val START_AUDIO_PICKER = "startAudioPicker"
            const val TOKEN = "Token"
            const val GET_DEVICEINFO = "getDeviceInfo"
            const val SAVE_H5_INFO = "saveH5Info"
            const val GET_H5_SAVE_INFO = "getH5SavaInfo"
        }
    }

    interface DEV_RUN_STATUS{
        companion object{
            const val FAULT = -1
            const val INITIAL = 1
            const val ONLINE = 2
            const val OPEN = 3
            const val WORKING = 4
        }
    }

}